package cn.lixin.org.controller;

import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.PageList;
import cn.lixin.org.domain.DictionaryDetail;
import cn.lixin.org.query.DictionaryDetailQuery;
import cn.lixin.org.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@ResponseBody(转换json)
@RequestMapping("/detail")
@Api(tags = "字典详情接口",description = "字典详情接口详细描述")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "根据ID查询类型",notes = "查询所有字典详情")
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.loadAll();
    }

    /**
     * 根据id查询字典详情
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个字典详情",notes = "根据id查询字典详情")
    public DictionaryDetail queryById(@PathVariable("id") Long id){ // PathVariable：一个{}的时候可以不写
        System.out.println(dictionaryDetailService.loadById(id));
        return dictionaryDetailService.loadById(id);
    }

    /**
     * 高级查询
     * @return
     * RequestBody: 如果请求的是一个对象，则用RequestBody ，请求体
     *  axios get不支持传对象
     */
    @PostMapping
    @ApiOperation(value = "高级查询",notes = "根据query高级查询")
    public PageList<DictionaryDetail> pageList(@RequestBody DictionaryDetailQuery query){ // PathVariable：一个{}的时候可以不写
        return dictionaryDetailService.queryPage(query);
    }

    /**
     * 修改添加
     * PutMapping: 修改或添加
     * @param dictionaryDetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "字典详情添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        System.out.println(dictionaryDetail);
        try {
            if (dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 删除
     * DeleteMapping: 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
           dictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    @PostMapping("/details")
    @ApiOperation(value = "根据id查询抽屉中的类型",notes = "根据id")
    public List<DictionaryDetail> loadDicType(@RequestBody Long id){ //
        System.out.println(id);
        System.out.println(dictionaryDetailService.loadDicType(id));
        return dictionaryDetailService.loadDicType(id);
    }

    /**
     * 删除所有
     *
     * @param ids
     * RequestBody : 数组也是对象
     */
    @PatchMapping
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult removeList(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.removeList(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

}
